
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>audioop --- Manipulate raw audio data &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="aifc --- Read and write AIFF and AIFC files" href="aifc.html" />
    <link rel="prev" title="多媒体服务" href="mm.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/audioop.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="aifc.html" title="aifc --- Read and write AIFF and AIFC files"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="mm.html" title="多媒体服务"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="mm.html" accesskey="U">多媒体服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-audioop">
<span id="audioop-manipulate-raw-audio-data"></span><h1><a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> --- Manipulate raw audio data<a class="headerlink" href="#module-audioop" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-audioop" title="audioop: Manipulate raw audio data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">audioop</span></code></a> module contains some useful operations on sound fragments.
It operates on sound fragments consisting of signed integer samples 8, 16, 24
or 32 bits wide, stored in <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>.  All scalar items are
integers, unless specified otherwise.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>Support for 24-bit samples was added.
All functions now accept any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.
String input now results in an immediate error.</p>
</div>
<p id="index-0">This module provides support for a-LAW, u-LAW and Intel/DVI ADPCM encodings.</p>
<p>A few of the more complicated operations only take 16-bit samples, otherwise the
sample size (in bytes) is always a parameter of the operation.</p>
<p>The module defines the following variables and functions:</p>
<dl class="exception">
<dt id="audioop.error">
<em class="property">exception </em><code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">error</code><a class="headerlink" href="#audioop.error" title="永久链接至目标">¶</a></dt>
<dd><p>This exception is raised on all errors, such as unknown number of bytes per
sample, etc.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.add">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">add</code><span class="sig-paren">(</span><em class="sig-param">fragment1</em>, <em class="sig-param">fragment2</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.add" title="永久链接至目标">¶</a></dt>
<dd><p>Return a fragment which is the addition of the two samples passed as parameters.
<em>width</em> is the sample width in bytes, either <code class="docutils literal notranslate"><span class="pre">1</span></code>, <code class="docutils literal notranslate"><span class="pre">2</span></code>, <code class="docutils literal notranslate"><span class="pre">3</span></code> or <code class="docutils literal notranslate"><span class="pre">4</span></code>.  Both
fragments should have the same length.  Samples are truncated in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.adpcm2lin">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">adpcm2lin</code><span class="sig-paren">(</span><em class="sig-param">adpcmfragment</em>, <em class="sig-param">width</em>, <em class="sig-param">state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.adpcm2lin" title="永久链接至目标">¶</a></dt>
<dd><p>Decode an Intel/DVI ADPCM coded fragment to a linear fragment.  See the
description of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a> for details on ADPCM coding. Return a tuple
<code class="docutils literal notranslate"><span class="pre">(sample,</span> <span class="pre">newstate)</span></code> where the sample has the width specified in <em>width</em>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.alaw2lin">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">alaw2lin</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.alaw2lin" title="永久链接至目标">¶</a></dt>
<dd><p>Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.
a-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.avg">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">avg</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avg" title="永久链接至目标">¶</a></dt>
<dd><p>Return the average over all samples in the fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.avgpp">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">avgpp</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.avgpp" title="永久链接至目标">¶</a></dt>
<dd><p>Return the average peak-peak value over all samples in the fragment. No
filtering is done, so the usefulness of this routine is questionable.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.bias">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">bias</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">bias</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.bias" title="永久链接至目标">¶</a></dt>
<dd><p>Return a fragment that is the original fragment with a bias added to each
sample.  Samples wrap around in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.byteswap">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">byteswap</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.byteswap" title="永久链接至目标">¶</a></dt>
<dd><p>&quot;Byteswap&quot; all samples in a fragment and returns the modified fragment.
Converts big-endian samples to little-endian and vice versa.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.4 新版功能.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="audioop.cross">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">cross</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.cross" title="永久链接至目标">¶</a></dt>
<dd><p>Return the number of zero crossings in the fragment passed as an argument.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findfactor">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">findfactor</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfactor" title="永久链接至目标">¶</a></dt>
<dd><p>Return a factor <em>F</em> such that <code class="docutils literal notranslate"><span class="pre">rms(add(fragment,</span> <span class="pre">mul(reference,</span> <span class="pre">-F)))</span></code> is
minimal, i.e., return the factor with which you should multiply <em>reference</em> to
make it match as well as possible to <em>fragment</em>.  The fragments should both
contain 2-byte samples.</p>
<p>The time taken by this routine is proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findfit">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">findfit</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">reference</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findfit" title="永久链接至目标">¶</a></dt>
<dd><p>Try to match <em>reference</em> as well as possible to a portion of <em>fragment</em> (which
should be the longer fragment).  This is (conceptually) done by taking slices
out of <em>fragment</em>, using <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a> to compute the best match, and
minimizing the result.  The fragments should both contain 2-byte samples.
Return a tuple <code class="docutils literal notranslate"><span class="pre">(offset,</span> <span class="pre">factor)</span></code> where <em>offset</em> is the (integer) offset into
<em>fragment</em> where the optimal match started and <em>factor</em> is the (floating-point)
factor as per <a class="reference internal" href="#audioop.findfactor" title="audioop.findfactor"><code class="xref py py-func docutils literal notranslate"><span class="pre">findfactor()</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.findmax">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">findmax</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">length</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.findmax" title="永久链接至目标">¶</a></dt>
<dd><p>Search <em>fragment</em> for a slice of length <em>length</em> samples (not bytes!) with
maximum energy, i.e., return <em>i</em> for which <code class="docutils literal notranslate"><span class="pre">rms(fragment[i*2:(i+length)*2])</span></code>
is maximal.  The fragments should both contain 2-byte samples.</p>
<p>The routine takes time proportional to <code class="docutils literal notranslate"><span class="pre">len(fragment)</span></code>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.getsample">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">getsample</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">index</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.getsample" title="永久链接至目标">¶</a></dt>
<dd><p>Return the value of sample <em>index</em> from the fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2adpcm">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">lin2adpcm</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">state</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2adpcm" title="永久链接至目标">¶</a></dt>
<dd><p>Convert samples to 4 bit Intel/DVI ADPCM encoding.  ADPCM coding is an adaptive
coding scheme, whereby each 4 bit number is the difference between one sample
and the next, divided by a (varying) step.  The Intel/DVI ADPCM algorithm has
been selected for use by the IMA, so it may well become a standard.</p>
<p><em>state</em> is a tuple containing the state of the coder.  The coder returns a tuple
<code class="docutils literal notranslate"><span class="pre">(adpcmfrag,</span> <span class="pre">newstate)</span></code>, and the <em>newstate</em> should be passed to the next call
of <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>.  In the initial call, <code class="docutils literal notranslate"><span class="pre">None</span></code> can be passed as the state.
<em>adpcmfrag</em> is the ADPCM coded fragment packed 2 4-bit values per byte.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2alaw">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">lin2alaw</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2alaw" title="永久链接至目标">¶</a></dt>
<dd><p>Convert samples in the audio fragment to a-LAW encoding and return this as a
bytes object.  a-LAW is an audio encoding format whereby you get a dynamic
range of about 13 bits using only 8 bit samples.  It is used by the Sun audio
hardware, among others.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2lin">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">lin2lin</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">newwidth</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2lin" title="永久链接至目标">¶</a></dt>
<dd><p>Convert samples between 1-, 2-, 3- and 4-byte formats.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>In some audio formats, such as .WAV files, 16, 24 and 32 bit samples are
signed, but 8 bit samples are unsigned.  So when converting to 8 bit wide
samples for these formats, you need to also add 128 to the result:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">lin2lin</span><span class="p">(</span><span class="n">frames</span><span class="p">,</span> <span class="n">old_width</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">new_frames</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">bias</span><span class="p">(</span><span class="n">new_frames</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span>
</pre></div>
</div>
<p>The same, in reverse, has to be applied when converting from 8 to 16, 24
or 32 bit width samples.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="audioop.lin2ulaw">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">lin2ulaw</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.lin2ulaw" title="永久链接至目标">¶</a></dt>
<dd><p>Convert samples in the audio fragment to u-LAW encoding and return this as a
bytes object.  u-LAW is an audio encoding format whereby you get a dynamic
range of about 14 bits using only 8 bit samples.  It is used by the Sun audio
hardware, among others.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.max">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">max</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.max" title="永久链接至目标">¶</a></dt>
<dd><p>Return the maximum of the <em>absolute value</em> of all samples in a fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.maxpp">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">maxpp</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.maxpp" title="永久链接至目标">¶</a></dt>
<dd><p>Return the maximum peak-peak value in the sound fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.minmax">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">minmax</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.minmax" title="永久链接至目标">¶</a></dt>
<dd><p>Return a tuple consisting of the minimum and maximum values of all samples in
the sound fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.mul">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">mul</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">factor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.mul" title="永久链接至目标">¶</a></dt>
<dd><p>Return a fragment that has all samples in the original fragment multiplied by
the floating-point value <em>factor</em>.  Samples are truncated in case of overflow.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.ratecv">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">ratecv</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">nchannels</em>, <em class="sig-param">inrate</em>, <em class="sig-param">outrate</em>, <em class="sig-param">state</em><span class="optional">[</span>, <em class="sig-param">weightA</em><span class="optional">[</span>, <em class="sig-param">weightB</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ratecv" title="永久链接至目标">¶</a></dt>
<dd><p>Convert the frame rate of the input fragment.</p>
<p><em>state</em> is a tuple containing the state of the converter.  The converter returns
a tuple <code class="docutils literal notranslate"><span class="pre">(newfragment,</span> <span class="pre">newstate)</span></code>, and <em>newstate</em> should be passed to the next
call of <a class="reference internal" href="#audioop.ratecv" title="audioop.ratecv"><code class="xref py py-func docutils literal notranslate"><span class="pre">ratecv()</span></code></a>.  The initial call should pass <code class="docutils literal notranslate"><span class="pre">None</span></code> as the state.</p>
<p>The <em>weightA</em> and <em>weightB</em> arguments are parameters for a simple digital filter
and default to <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code> respectively.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.reverse">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">reverse</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.reverse" title="永久链接至目标">¶</a></dt>
<dd><p>Reverse the samples in a fragment and returns the modified fragment.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.rms">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">rms</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.rms" title="永久链接至目标">¶</a></dt>
<dd><p>Return the root-mean-square of the fragment, i.e. <code class="docutils literal notranslate"><span class="pre">sqrt(sum(S_i^2)/n)</span></code>.</p>
<p>This is a measure of the power in an audio signal.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.tomono">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">tomono</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">lfactor</em>, <em class="sig-param">rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tomono" title="永久链接至目标">¶</a></dt>
<dd><p>Convert a stereo fragment to a mono fragment.  The left channel is multiplied by
<em>lfactor</em> and the right channel by <em>rfactor</em> before adding the two channels to
give a mono signal.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.tostereo">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">tostereo</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em>, <em class="sig-param">lfactor</em>, <em class="sig-param">rfactor</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.tostereo" title="永久链接至目标">¶</a></dt>
<dd><p>Generate a stereo fragment from a mono fragment.  Each pair of samples in the
stereo fragment are computed from the mono sample, whereby left channel samples
are multiplied by <em>lfactor</em> and right channel samples by <em>rfactor</em>.</p>
</dd></dl>

<dl class="function">
<dt id="audioop.ulaw2lin">
<code class="sig-prename descclassname">audioop.</code><code class="sig-name descname">ulaw2lin</code><span class="sig-paren">(</span><em class="sig-param">fragment</em>, <em class="sig-param">width</em><span class="sig-paren">)</span><a class="headerlink" href="#audioop.ulaw2lin" title="永久链接至目标">¶</a></dt>
<dd><p>Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.
u-LAW encoding always uses 8 bits samples, so <em>width</em> refers only to the sample
width of the output fragment here.</p>
</dd></dl>

<p>Note that operations such as <a class="reference internal" href="#audioop.mul" title="audioop.mul"><code class="xref py py-func docutils literal notranslate"><span class="pre">mul()</span></code></a> or <a class="reference internal" href="#audioop.max" title="audioop.max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a> make no distinction
between mono and stereo fragments, i.e. all samples are treated equal.  If this
is a problem the stereo fragment should be split into two mono fragments first
and recombined later.  Here is an example of how to do that:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">mul_stereo</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">):</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tomono</span><span class="p">(</span><span class="n">sample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">lfactor</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="n">rfactor</span><span class="p">)</span>
    <span class="n">lsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
    <span class="n">rsample</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">tostereo</span><span class="p">(</span><span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">lsample</span><span class="p">,</span> <span class="n">rsample</span><span class="p">,</span> <span class="n">width</span><span class="p">)</span>
</pre></div>
</div>
<p>If you use the ADPCM coder to build network packets and you want your protocol
to be stateless (i.e. to be able to tolerate packet loss) you should not only
transmit the data but also the state.  Note that you should send the <em>initial</em>
state (the one you passed to <a class="reference internal" href="#audioop.lin2adpcm" title="audioop.lin2adpcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lin2adpcm()</span></code></a>) along to the decoder, not the
final state (as returned by the coder).  If you want to use
<a class="reference internal" href="struct.html#struct.Struct" title="struct.Struct"><code class="xref py py-class docutils literal notranslate"><span class="pre">struct.Struct</span></code></a> to store the state in binary you can code the first
element (the predicted value) in 16 bits and the second (the delta index) in 8.</p>
<p>The ADPCM coders have never been tried against other ADPCM coders, only against
themselves.  It could well be that I misinterpreted the standards in which case
they will not be interoperable with the respective standards.</p>
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">find*()</span></code> routines might look a bit funny at first sight. They are
primarily meant to do echo cancellation.  A reasonably fast way to do this is to
pick the most energetic piece of the output sample, locate that in the input
sample and subtract the whole output sample from the input sample:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">echocancel</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="n">inputdata</span><span class="p">):</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findmax</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">800</span><span class="p">)</span>    <span class="c1"># one tenth second</span>
    <span class="n">out_test</span> <span class="o">=</span> <span class="n">outputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
    <span class="n">in_test</span> <span class="o">=</span> <span class="n">inputdata</span><span class="p">[</span><span class="n">pos</span><span class="o">*</span><span class="mi">2</span><span class="p">:]</span>
    <span class="n">ipos</span><span class="p">,</span> <span class="n">factor</span> <span class="o">=</span> <span class="n">audioop</span><span class="o">.</span><span class="n">findfit</span><span class="p">(</span><span class="n">in_test</span><span class="p">,</span> <span class="n">out_test</span><span class="p">)</span>
    <span class="c1"># Optional (for better cancellation):</span>
    <span class="c1"># factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],</span>
    <span class="c1">#              out_test)</span>
    <span class="n">prefill</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="o">*</span><span class="p">(</span><span class="n">pos</span><span class="o">+</span><span class="n">ipos</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span>
    <span class="n">postfill</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\0</span><span class="s1">&#39;</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">inputdata</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">prefill</span><span class="p">)</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">outputdata</span><span class="p">))</span>
    <span class="n">outputdata</span> <span class="o">=</span> <span class="n">prefill</span> <span class="o">+</span> <span class="n">audioop</span><span class="o">.</span><span class="n">mul</span><span class="p">(</span><span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="n">factor</span><span class="p">)</span> <span class="o">+</span> <span class="n">postfill</span>
    <span class="k">return</span> <span class="n">audioop</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">inputdata</span><span class="p">,</span> <span class="n">outputdata</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
</pre></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="mm.html"
                        title="上一章">多媒体服务</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="aifc.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">aifc</span></code> --- Read and write AIFF and AIFC files</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/audioop.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="aifc.html" title="aifc --- Read and write AIFF and AIFC files"
             >下一页</a> |</li>
        <li class="right" >
          <a href="mm.html" title="多媒体服务"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="mm.html" >多媒体服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>